package com.example.backend.service;

import com.example.backend.mapper.DingDanDetailMapper;
import com.example.backend.mapper.DingDanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Service
public class IncomeService {
    @Autowired
    private DingDanMapper dingDanMapper;
    
    @Autowired
    private DingDanDetailMapper dingDanDetailMapper;

    public Map<String, Object> getDishSales(Integer shopId) {
        // 获取已完成的订单号
        List<String> orderNumbers = dingDanMapper.getOrderNumbersByShopAndState(shopId, "5");

        if (orderNumbers.isEmpty()) {
            return Map.of("data", Map.of());
        }

        // 统计菜品销售数量
        List<Map<String, Object>> resultList = dingDanDetailMapper.countDishSalesByOrderNumbers(orderNumbers);

        // 转换为 Map<String, Integer> 格式
        Map<String, Integer> dishSales = new HashMap<>();
        for (Map<String, Object> row : resultList) {
            String dishName = (String) row.get("dish_name");
            Number totalCount = (Number) row.get("total_count");
            dishSales.put(dishName, totalCount.intValue());
        }

        return Map.of("data", dishSales);
    }

    public Map<String, Object> getTotalIncome(Integer shopId) {
        // 获取已完成的订单号
        List<String> orderNumbers = dingDanMapper.getOrderNumbersByShopAndState(shopId, "5");

        double totalIncome = 0.0;
        if (!orderNumbers.isEmpty()) {
            List<Map<String, Object>> resultList = dingDanDetailMapper.calculateTotalIncomeByOrderNumbers(orderNumbers);
            if (!resultList.isEmpty() && !resultList.get(0).isEmpty()) {
                Map<String, Object> row = resultList.get(0);
                Object totalIncomeObj = row.get("total_income");
                if (totalIncomeObj != null) {
                    Number totalIncomeNumber = (Number) totalIncomeObj;
                    totalIncome = totalIncomeNumber.doubleValue();
                }
            }
        }

        return Map.of("data", Map.of("totalIncome", totalIncome));
    }

    public Map<String, Object> getDailyDishSales(Integer shopId, String date) {
        // 获取指定日期已完成的订单号
        List<String> orderNumbers = dingDanMapper.getOrderNumbersByShopAndStateAndDate(shopId, "5", date);

        if (orderNumbers.isEmpty()) {
            return Map.of("data", Map.of());
        }

        // 统计菜品销售数量
        List<Map<String, Object>> resultList = dingDanDetailMapper.countDishSalesByOrderNumbers(orderNumbers);

        // 转换为 Map<String, Integer> 格式
        Map<String, Integer> dishSales = new HashMap<>();
        for (Map<String, Object> row : resultList) {
            String dishName = (String) row.get("dish_name");
            Number totalCount = (Number) row.get("total_count");
            dishSales.put(dishName, totalCount.intValue());
        }

        return Map.of("data", dishSales);
    }

    public Map<String, Object> getDailyTotalIncome(Integer shopId, String date) {
        // 获取指定日期已完成的订单号
        List<String> orderNumbers = dingDanMapper.getOrderNumbersByShopAndStateAndDate(shopId, "5", date);

        double totalIncome = 0.0;
        if (!orderNumbers.isEmpty()) {
            List<Map<String, Object>> resultList = dingDanDetailMapper.calculateTotalIncomeByOrderNumbers(orderNumbers);
            if (!resultList.isEmpty() && !resultList.get(0).isEmpty()) {
                Map<String, Object> row = resultList.get(0);
                Object totalIncomeObj = row.get("total_income");
                if (totalIncomeObj != null) {
                    Number totalIncomeNumber = (Number) totalIncomeObj;
                    totalIncome = totalIncomeNumber.doubleValue();
                }
            }
        }

        return Map.of("data", Map.of("totalIncome", totalIncome));
    }
}